﻿package com.crl.module.cmpRpc.behavior
{
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.SecurityErrorEvent;
	import flash.events.TextEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	/**
	 * ...
	 * @author crl
	 */
	public class HttpRequest extends AbstractRequestBehavior
	{
		private var loader:URLLoader;
		private var _data:Object;
		
		private var _request:URLRequest;
		private var _url:String
		public function HttpRequest(url:String) 
		{
			this._url=url;
			loader = new URLLoader();
			loader.addEventListener(Event.COMPLETE,completeHandler);
			loader.addEventListener(IOErrorEvent.IO_ERROR,errorEvent);
			loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR,errorEvent);
			
			_request=new URLRequest();
		}
		
		
		private function completeHandler(event:Event):void{
			
			this.resultHandler((event.target as URLLoader).data);
		}
		
		private function errorEvent(event:TextEvent):void{
			//(event as IOErrorEvent).text;
			//(event as SecurityErrorEvent).text
			this.errorHandler(event.text);
		}
		
		override public function request(o:Object=null):RemoteToken {
			_request.url=_url;
			_request.data=o;
			loader.load(_request);
			return token;
		}
		
	}

}